import AesUtil from "../../utils/AesUtil"
import request from '../../utils/request.js'

export default {
	state: {
		id: '',
		username: '',
		password: '',
		phone: '',
		role: '',
		roleName: '',
		nickname: '',
		avatar: '',
		sex: '',
		birth: '',
		createTime: '',
		token: '',
	},
	mutations: {
		SET_ID: (state, data) => {
			state.id = AesUtil.encrypt(data)
		},
		SET_USERNAME: (state, data) => {
			state.username = AesUtil.encrypt(data)
		},
		SET_PASSWORD: (state, data) => {
			state.password = AesUtil.encrypt(data)
		},
		SET_PHONE: (state, data) => {
			state.phone = AesUtil.encrypt(data)
		},
		SET_ROLE: (state, data) => {
			state.role = AesUtil.encrypt(data)
		},
		SET_ROLENAME: (state, data) => {
			state.roleName = AesUtil.encrypt(data)
		},
		SET_NICKNAME: (state, data) => {
			state.nickname = AesUtil.encrypt(data)
		},
		SET_AVATAR: (state, data) => {
			state.avatar = AesUtil.encrypt(data)
		},
		SET_SEX: (state, data) => {
			state.sex = AesUtil.encrypt(data)
		},
		SET_BIRTH: (state, data) => {
			state.birth = AesUtil.encrypt(data)
		},
		SET_CREATE_TIME: (state, data) => {
			state.createTime = AesUtil.encrypt(data)
		},
		SET_ALL: (state, data) => {
			Object.keys(data).forEach(key => {
				state[key] = AesUtil.encrypt(data[key])
				uni.$store.set(key, data[key])
			})
		},
		SET_TOKEN: (state, data) => {
			state.token = data
			uni.$store.set('token', data)
		},
		GET_STORE: (state) => {
			Object.keys(state).forEach(key => {
				state[key] = uni.$store.get(key)
			})
		}
	},
	actions: {
		STORE_USER_INFO: ({ commit }, data) => {
			commit('SET_ALL', data)
		},
		setToken: ({ commit }, data) => {
			commit('SET_TOKEN', data)
		},
		getInfo: (store) => {
			request.post('/user/info').then(res => {
				if(res.code === 200) {
					store.commit('SET_ALL', res.data)
					store.dispatch('connectSocket', res.data.id)
				}
			})
		}
	}
}